package com.example.minaim;

import java.io.Serializable;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;

import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import android.R.integer;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.example.minaim.ClientHandler.Callback;
import com.example.minaim.Define.ClientStatus;
import com.example.minaim.MainActivity.MyReceiver;

public class skillsActivity extends Activity {


	private Define define;
	
	private MyReceiver receiver;
	
	private User userInfo;
	//private List<BaseSkill> baseskills;
	//private HashMap<String, BaseSkill> baseskills;
	
	public ClientStatus clientStatus = ClientStatus.idle;

	
	private HashMap<String, Skill> allSkills;
	private List<Skill> allSkillsList;
	private Button btnSkillLearn;
	private Button btnSkillCancle;
	private ListView listSkill;
	private TextView txtSkillsDesc;
	
	private int Select = -1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//閸樼粯甯�弽鍥暯閺嶏拷
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				  WindowManager.LayoutParams.FLAG_FULLSCREEN);//鐠佸墽鐤嗛崗銊ョ潌
		super.onCreate(savedInstanceState);
		setContentView(R.layout.skills_main);

		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		allSkills = (HashMap<String, Skill>) bundle.getSerializable("allSkills");
		allSkillsList = (List<Skill>) bundle.getSerializable("allSkillsList");
		userInfo = (User) bundle.getSerializable("userInfo");
		
		receiver = new MyReceiver();                
		IntentFilter filter = new IntentFilter();		
		filter.addAction("com.minam.test");		
		skillsActivity.this.registerReceiver(receiver, filter);	
		
		btnSkillLearn = (Button) findViewById(R.id.btnSkillLearn);
		btnSkillCancle = (Button) findViewById(R.id.btnSkillCancle);
		listSkill = (ListView) findViewById(R.id.listSkill);
		txtSkillsDesc = (TextView) findViewById(R.id.txtSkillsDesc);		
		
		/*
		String[] skillsString = new String[allSkills.size()];
		
		for (int x = 0; x < this.allSkills.size(); x++) {
			skillsString[x] = this.allSkills.get(x).get_name();
		}
		
		listSkill.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, skillsString));
		*/
		/*
		List<Skill> skillList = null;
		for (int x = 0; x < this.allSkills.size(); x++) {
			skillList.add(this.allSkills.get(x));
		}
		*/
		skillAdapter adapter = new skillAdapter(this, allSkillsList,userInfo);
		listSkill.setAdapter(adapter);

		this.listSkill.setOnItemClickListener(new OnItemClickListenerImpl()); // 鍗曞嚮閫夐」
		
		
		btnSkillLearn.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
		        String GRoleInfoCmd="GROLEINFO:";				
				
				Intent intent = new Intent();	
				intent.putExtra("cmd","send");
				intent.putExtra("body",GRoleInfoCmd);
				intent.setAction("com.minam.client");// action娑撳孩甯撮弨璺烘珤閻╃鎮�	
				sendBroadcast(intent);
				clientStatus = ClientStatus.roleinfoing;
			}
		});
		
		btnSkillCancle.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				finish();
			}
		});
		
	}
	
	private class OnItemClickListenerImpl implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			txtSkillsDesc.setText(allSkillsList.get(arg2).get_desc());
			Select = arg2;
		}
	}
	
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // 检测屏幕的方向：纵向或横向
        if (this.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            //当前为横屏， 在此处添加额外的处理代码
        }
        else if (this.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_PORTRAIT) {
            //当前为竖屏， 在此处添加额外的处理代码
        }
        //检测实体键盘的状态：推出或者合上
        if (newConfig.hardKeyboardHidden
                == Configuration.HARDKEYBOARDHIDDEN_NO){
            //实体键盘处于推出状态，在此处添加额外的处理代码
        }
        else if (newConfig.hardKeyboardHidden
                == Configuration.HARDKEYBOARDHIDDEN_YES){
            //实体键盘处于合上状态，在此处添加额外的处理代码
        }
    }
	
	
	public class MyReceiver extends BroadcastReceiver
    {
        
        // 閼奉亜鐣炬稊澶夌娑擃亜绠嶉幘顓熷复閺�泛娅�        
        @Override
        public void onReceive(Context context, Intent intent)
        {
            
            // TODO Auto-generated method stub
            
            System.out.println("tianfuActivity OnReceiver");
            
            Bundle bundle = intent.getExtras();
            
            String cmd = bundle.getString("cmd");
            
            if (cmd.equals("receive")) {
            	String body = bundle.getString("body");
            	Cmd2vector cmd2vector = new Cmd2vector();
            	cmd2vector.cmd2vector(body);

            	if (cmd2vector.cmd.equals("c05")) {
					//String result = cmd2vector.paras.get("f");
            		//c05&total=2&number=1&name=婵繐绠掗埀顒�噹閵囧鎸�desc=闁圭鍋撻柡鍫濐樇閺侀箖宕欑紒妯何楅柤瀹犲Г閺侀箖宕欑拠鎻掝瀻濠⒀呭仜婵拷%
            		
				}
            }

        }
        
        public MyReceiver()
        {
            
            System.out.println("MyReceiver");
            
            // 閺嬪嫰锟介崙鑺ユ殶閿涘苯浠涙稉锟界昂閸掓繂顬婇崠鏍т紣娴ｆ粣绱濋張顑跨伐娑擃厽妫ゆ禒璁崇秿娴ｆ粎鏁�            
        }
        
    }
	
}
